//
//  RSDBRecord.h
//  Cellar Tracker
//
//  Created by Johnnie Walker on 09/07/2008.
//  Copyright 2008 Random Sequence. All rights reserved.
//

#import <UIKit/UIKit.h>

@class RSDBTable;
@class RSDBRelation;
@class RSDBDatabase;
@class RSDBResultSet;

@interface RSDBRecord : NSObject {
	RSDBTable *table;	
	RSDBDatabase *database;
	BOOL exists;

	NSMutableDictionary *relations;	
	NSMutableDictionary *properties;
	NSMutableDictionary *changedProperties;	
}

@property (nonatomic, retain) RSDBTable *table;
@property (nonatomic, retain) RSDBDatabase *database;
@property (nonatomic, retain) NSMutableDictionary *properties;
@property (nonatomic) BOOL exists;

+ (RSDBTable *)table;
+ (RSDBRecord *)find:(id)identifier;
+ (NSArray *)findAll;
+ (NSArray *)findBySQL:(NSString *)sql, ...;
+ (RSDBRecord *)findOneBySQL:(NSString *)sql, ...;

- (id)initWithTable:(RSDBTable *)table;
- (id)initWithTable:(RSDBTable *)table resultsFromSet:(RSDBResultSet *)resultSet;

- (id)identifier;

- (id)getProperty:(NSString *)property;
- (id)valueForProperty:(NSString *)property;
- (void)setValue:(id)value forProperty:(NSString *)property;

- (void)addRelation:(RSDBRelation *)relation alias:(NSString *)alias;
- (BOOL)hasRelation:(NSString *)relationName;
- (RSDBRelation *)relationForAlias:(NSString *)alias;

- (RSDBRelation *)magicHasManyRelationForAlias:(NSString *)alias;
- (RSDBRelation *)magicBelongsToOneRelationForAlias:(NSString *)alias;

- (id)getRelatedRecord:(NSString *)alias;
- (id)relatedRecordForProperty:(NSString *)alias;
- (void)setRelatedRecord:(RSDBRecord *)record forProperty:(NSString *)alias;

- (NSArray *)getRelatedRecords:(NSString *)alias;
- (void)mergeWithDictionary:(NSDictionary *)dictionary;

- (BOOL)save;
- (void)preInsert;
- (void)postInsert;
- (void)preUpdate;
- (void)postUpdate;
- (void)preSave;
- (void)postSave;
- (BOOL)delete;
- (void)preDelete;
- (void)postDelete;

@end
